package com.niodata.md.rdb.crawler.plugin;

import com.niodata.md.rdb.crawler.AbstractRetrieval;
import com.niodata.md.rdb.crawler.MetaDataRetrieval;
import com.niodata.md.rdb.model.Table;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

/**
 * doris doris row count retrieval.
 */
public class DorisRowCountRetrieval extends AbstractRetrieval {
  @Override
  public List<MetaDataRetrieval> retrieveMetaData() throws Exception {
    String sql = "select TABLE_ROWS from information_schema.tables where TABLE_SCHEMA='%s' and TABLE_NAME='%s'";
    Table table = this.getCatalog().getTable(this.getParentPath());
    sql = String.format(sql, table.getDatabase(), table.getName());
    Connection connection = this.getDataSourceInfo().getConnection();
    try {
      Statement statement = connection.createStatement();
      ResultSet rs = statement.executeQuery(sql);
      rs.next();
      table.setRowCount(rs.getLong(1));
    } finally {
      connection.close();
    }
    return List.of();
  }
}
